package com.preresearch.recyclerview.hongyang;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.preresearch.recyclerview.R;
import com.preresearch.recyclerview.decoration.DividerItemDecoration;

import java.util.List;

/**
 * 参考鸿洋demo：
 * http://blog.csdn.net/lmj623565791/article/details/45059587
 */
public class HomeActivity extends ActionBarActivity {

    private RecyclerView mRecyclerView;
    private ListView mLv;

    private List<String> mDatas;
//    private HomeAdapter mAdapter;

    private String[] urls = {
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_1.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_2.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_3.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_4.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_5.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_6.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_7.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_8.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_9.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_10.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_11.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_12.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_13.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_14.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_15.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_16.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_17.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_18.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_19.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_20.png",
            "http://images.cnblogs.com/cnblogs_com/zhzhy126/729387/t_21.png"
    };
    private MyListAdapter mListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single_recyclerview);
        initView();
        initData();
        initListener();
    }

    private void initView() {
//        mRecyclerView = (RecyclerView) findViewById(R.id.id_recyclerview);
        mLv = (ListView) findViewById(R.id.lv);
    }

    private void initData() {
        // 类似GridView， 4列
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.HORIZONTAL_LIST));

        mListAdapter = new MyListAdapter();
    }

    private void initListener() {
//        mRecyclerView.setAdapter(mAdapter = new HomeAdapter());
        mLv.setAdapter(mListAdapter);
    }


    private class MyListAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return urls.length;
        }

        @Override
        public Object getItem(int position) {
            return urls[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(HomeActivity.this).inflate(R.layout.item_home, null);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv);
                holder.iv.setTag(urls[position]);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            ImageLoader.getInstance().displayImage(urls[position], holder.iv);

            return convertView;
        }

        private class ViewHolder {
            ImageView iv;
        }
    }









//    class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.MyViewHolder> {
//
//        @Override
//        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//            MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
//                    HomeActivity.this).inflate(R.layout.item_home, parent,
//                    false));
//            return holder;
//        }
//
//        @Override
//        public void onBindViewHolder(MyViewHolder holder, int position) {
//            String url = urls[position];
//            Uri uri = Uri.parse(url);
//
//            ImageLoader.getInstance().displayImage(uri, iv);
//        }
//
//        @Override
//        public int getItemCount() {
//            return mDatas.size();
//        }
//
//        class MyViewHolder extends ViewHolder {
//            ImageView iv;
//
//            public MyViewHolder(View view) {
//                super(view);
//                iv = (ImageView) view.findViewById(R.id.iv);
//            }
//        }
//    }

}